package com.ciban.smarthome.module.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.DensityUtils;
import com.ciban.smarthome.common.utils.ImgUtils;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.engine.LampEngine;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.entity.Equip_Lamp;

public class E_LampModifyActivity extends BaseActivity implements
        OnClickListener {
    public static final int INTENT_CHOOSE_ICON = 101;

    private EditText et_id, et_name;
    private CheckBox cb_ktg;
    private ImageView iv_icon;
    private Button bt_cancle, bt_save;

    private String icon_path= "imgs/light_icon_bd.png";

    private boolean isEdit = false;
    private Equip_Lamp light;

    private LampEngine engine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_equip_lamp_modify);

        int width = DensityUtils.getWindowWidth(this);
        LayoutParams p = getWindow().getAttributes();
        p.width = (int) (width * 0.8);
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) p);

        initData();

        initViews();
    }

    private void initData() {
        if (getIntent().hasExtra("light")) {
            light = (Equip_Lamp) getIntent().getExtras().get("light");
            isEdit = true;
        }else if(getIntent().hasExtra("type")){
            light=new Equip_Lamp();
            light.setType(getIntent().getIntExtra("type",-1));
        }

        engine = new LampEngine(this);
    }

    private void initViews() {
        et_id = (EditText) findViewById(R.id.light_add_no);
        et_name = (EditText) findViewById(R.id.light_add_name);
        cb_ktg = (CheckBox) findViewById(R.id.light_add_ktg);
        iv_icon = (ImageView) findViewById(R.id.light_add_icon);
        bt_cancle = (Button) findViewById(R.id.light_add_cancle);
        bt_save = (Button) findViewById(R.id.light_add_save);

        if (isEdit) {
            LogUtil.info("需要修改的电灯的信息："+light.toString());

            et_id.setText(light.getSerial_no() + "");
            et_name.setText(light.getName());
            if (light.getLevel() >= 0) {
                cb_ktg.setChecked(true);
            }
            icon_path = light.getIcon_path();
            Bitmap bp = ImgUtils.getImageFromAssetsFile(this, icon_path);
            iv_icon.setImageBitmap(bp);
        }

        setListeners();
    }

    private void setListeners() {
        iv_icon.setOnClickListener(this);
        bt_cancle.setOnClickListener(this);
        bt_save.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.light_add_icon:
                Intent intent = new Intent(this, IconChooseActivity.class);
                intent.putExtra("ids",
                        getResources().getStringArray(R.array.light_icons));
                startActivityForResult(intent, INTENT_CHOOSE_ICON);
                break;
            case R.id.light_add_cancle:
                finish();
                break;
            case R.id.light_add_save:
                addLight();
                break;
        }
    }

    private void addLight() {
        String name = et_name.getText().toString().trim();
        String no = et_id.getText().toString().trim();

        int level = -1;
        if (cb_ktg.isChecked())
            level = 0;

        if (light == null)
            light = new Equip_Lamp();

        light.setArea_id(baseApp.getNowArea().getId());
        light.setSerial_no(Integer.parseInt(no));
        light.setName(name);
        light.setLevel(level);
        light.setIcon_path(icon_path);

        boolean result = false;
        if (isEdit) {
            if (engine.updateLamp(light)) {
                result = true;
            }
        } else if (engine.addNewLamp(light)) {
            result = true;
        }

        if (result) {
            setResult(RESULT_OK);
            Toast.makeText(this, "操作成功", Toast.LENGTH_SHORT).show();
            finish();
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case INTENT_CHOOSE_ICON:
                if (resultCode == RESULT_OK) {
                    icon_path = data.getStringExtra("path");
                    iv_icon.setImageBitmap(ImgUtils.getImageFromAssetsFile(this,
                            icon_path));
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        engine.destroy();
    }
}
